using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using System.Threading;
using InfraStructureLayer;
using EntityLayer;

public partial class _Default : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            LanguageLoad();
            try
            {
                if (Application["LastMapGeneratedDate"] == null)
                {
                    Application["LastMapGeneratedDate"] = DateTime.Now.Date;
                    new BusinessBase().UpdateSiteMap();
                }
                if (Convert.ToDateTime(Application["LastMapGeneratedDate"]) < DateTime.Now.Date)
                {
                    new BusinessBase().UpdateSiteMap();
                }
            }
            catch (Exception ex)
            {
                object[] mailParams = new object[] { ex.ToString() };
                busMails insBusMails = new busMails();
                insBusMails.SendMail(4, mailParams);
            }

        }
    }
    public override void LanguageLoad()
    {
        busEntries insBusEntries = new busEntries();
        DataTable insDataTableEntries = null;

        busNews insBusNews = new busNews();
        DataTable insDataTableNews = null;

        busArticles insBusArticles = new busArticles();
        DataTable insDataTableArticles = null;
        if (Request.QueryString["Year"] != null && Request.QueryString["Year"] != "" && Request.QueryString["Month"] != null && Request.QueryString["Month"] != "" && Request.QueryString["Day"] != null && Request.QueryString["Day"] != "")
        {
            DateTime dt = new DateTime(Convert.ToInt32(Request.QueryString["Year"]), Convert.ToInt32(Request.QueryString["Month"]), Convert.ToInt32(Request.QueryString["Day"]));
            entEntries insEntEntries = new entEntries();
            insEntEntries.PublishDate = dt;
            insDataTableEntries = insBusEntries.SelectEntriesByDateMainPage(insEntEntries);

            entNews insEntNews = new entNews();
            insEntNews.InsertDate = dt;
            insDataTableNews = insBusNews.SelectNewsByDateMainPage(insEntNews);

            entArticles insEntArticles = new entArticles();
            insEntArticles.PublishDate = dt;
            insDataTableArticles = insBusArticles.SelectArticlesByDateMainPage(insEntArticles);
        }
        else
        {
            insDataTableEntries = insBusEntries.SelectEntriesLatestMainPage();
            insDataTableNews = insBusNews.SelectNewsLatestMainPage();
            insDataTableArticles = insBusArticles.SelectArticlesLatestMainPage();
        }

        if (insDataTableEntries.Rows.Count > 0)
        {
            lblLatestEntryTitle.Text = insDataTableEntries.Rows[0]["Title"].ToString();
            lblLatestEntryBody.Text = insDataTableEntries.Rows[0]["Body"].ToString();
            Guid entryId = new Guid(insDataTableEntries.Rows[0]["PermalinkGUID"].ToString());
            a1.HRef = "~/" + CurrentContext.Language + "/blog/" + entryId.ToString() + ".aspx";
        }
        else
        {
            lblLatestEntryTitle.Text = "";
            lblLatestEntryBody.Text = StaticMethods.GetErrorMessageMultilanguageString(47, new object[] { });
        }


        if (insDataTableNews.Rows.Count > 0)
        {
            lblLatestNewsTitle.Text = insDataTableNews.Rows[0]["NewsTitle"].ToString();
            lblLatestNewsBody.Text = insDataTableNews.Rows[0]["NewsBody"].ToString();
            int newsId = Convert.ToInt32(insDataTableNews.Rows[0]["NewsId"].ToString());
            a2.HRef = "~/" + CurrentContext.Language + "/News/" + new Crypto().EncryptURL(newsId.ToString()) + ".aspx";
        }
        else
        {
            lblLatestNewsTitle.Text = "";
            lblLatestNewsBody.Text = StaticMethods.GetErrorMessageMultilanguageString(47, new object[] { });
        }


        if (insDataTableArticles.Rows.Count > 0)
        {
            lblLatestArticleTitle.Text = insDataTableArticles.Rows[0]["ArticleTitle"].ToString();
            lblLatestArticleBody.Text = insDataTableArticles.Rows[0]["ArticleSummary"].ToString();
            int articleId = Convert.ToInt32(insDataTableArticles.Rows[0]["ArticleId"].ToString());
            a3.HRef = "~/" + CurrentContext.Language + "/Articles/" + new Crypto().EncryptURL(articleId.ToString()) + ".aspx";
        }
        else
        {
            lblLatestArticleTitle.Text = "";
            lblLatestArticleBody.Text = StaticMethods.GetErrorMessageMultilanguageString(47, new object[] { });
        }
        base.LanguageLoad();
    }
    protected void lbtShowAllMainPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/" + CurrentContext.Language + "/Blog/default.aspx");
    }
    protected void lblShowAllNewsMainPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/" + CurrentContext.Language + "/news/default.aspx");
    }
    protected void lblShowAllArticlesMainPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/" + CurrentContext.Language + "/articles/default.aspx");
    }

}
